package Classes.GameViews// ActionScript file
{
	import Classes.GameDatas.BuildType;
	import Classes.GameDatas.GD_Building_Func;
	import Classes.GameDatas.GD_Building_Sundry;
	import Classes.GameDatas.GD_Character;
	import Classes.GameDatas.GD_HavenWall;
	import Classes.GameDatas.GD_HiredFriend;
	import Classes.GameDatas.GD_Map;
	import Classes.GameDatas.GD_MapGrid;
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Reward;
	import Classes.GameDatas.GD_SickMan;
	import Classes.GameDatas.GD_Zombie;
	import Classes.GameErrors.GameError;
	import Classes.GameRecords.GR_Build;
	import Classes.GameUtils.GD_deal;
	import Classes.GameUtils.GU_BarPathFinding;
	import Classes.GameUtils.GU_BuildingCursor;
	import Classes.GameUtils.GU_DataTransform;
	import Classes.GameUtils.GU_IsoUtils;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameUtils.GU_dealPower;
	import Classes.GameUtils.LoadImg;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_Building_Sundry;
	import Classes.GameViews.ViewDisplayers.MapTargets.MT_HavenWall;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MMT_Character;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapMoveTarget;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_SickMan;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MapNPCs.MN_Zombie;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapTarget;
	import Classes.GameViews.ViewDisplayers.VD_Reward;
	import Classes.GameViews.ViewPanels.VP_Main;
	import Classes.GameViews.ViewPanels.VP_TextWinPanel;
	import Classes.GameViews.ViewPanels.VP_movement;
	import Classes.Interfaces.I_Bar;
	import Classes.Interfaces.I_ControlTarget;
	import Classes.Interfaces.I_FindingTarget;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.Language;
	
	import UIComponents.PanelEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	public class GV_Map extends GameView implements I_FindingTarget
		
	{
		public static const SQRT2:Number=1.414;/*距离计算常数*/
		/**
		 * 回收模式
		 */		
		public static const CLEANMODE:String="cleanMode";
		/**
		 * 摆放模式 
		 */		
		public static const MOVEMODE:String="moveMode";
		/**
		 * 非建筑模式 
		 */		
		public static const CANNOTBUILDMODE:String="cannotBuildMode";
		
		private static var _instance:GV_Map;/*静态引用*/
		private static var _havenSetConfigDataAry:Array;/*避难所等级配置数据对象*/
		
		private var _waitingSelectTarget:MapTarget;/*等待选择的对象*/
		
		private var _needCheckWaitingSelectTargetSign:Boolean;/*检测等待选择对象标记*/
		private var _updateCount:Number=0;/*刷新计数*/
		
		private var _layerMapBackground:DisplayObject;/*地图背景显示层*/
		//private var _layerMapGrids:Sprite;/*地图格显示层*/
		//private var _layerMapLines:Sprite;/*地图标线层*/
		private var _layerClick:Sprite;/*地图点击层*/
		private var _layerSundries:Sprite;/*地图杂物层*/
		private var _layerMapTargets:Sprite;/*地图对象显示层*/
		private var _layerReward:Sprite;/*奖励掉落层*/
		private var _layerBuildingSet:GV_BuildingSetLayer;/*建筑摆放层*/
		private var _popTipsLayer:Sprite;/*泡泡窗口*/
		
		private var _gridSize:Number=10;/*地图格尺寸*/
		private var _gridsAry:Array;/*地图格数组*/
		private var _rewardAry:Array;/*掉落奖励*/
		private var _mapData:GD_Map;/*地图数据*/
		private var _controlTarget:I_ControlTarget;/*玩家控制对象*/
		private var _controlTargetObj:Object=new Object();/*玩家对象存储器*/
		
		private var _oldPositionPoint:Point;/*老的位置*/
		private var _dragRt:Rectangle;/*拖动范围矩形*/
		
		//private var _preventClick:Boolean;/*阻止点击*/	
		
		private var _isBuilding:Boolean;/*建设状态*/
		private var _buildMode:String=CANNOTBUILDMODE;/*建筑模式*/
		
		private var _wallLeavel:Number;/*围墙等级*/
		
		
		/** 2012.4.19 update by jamesge **/
		//private var _hitMouseTargetAry:Array;/*鼠标碰撞对象数组*/
		private var _frameNum:Number=0;
		private var _mapTargetsAry:Array;
		private var _floorTargetsAry:Array;
		/** 2012.4.19 update by jamesge **/
		
		/**
		 *改变鼠标指计的对像 
		 */
		private var _buildingCursor:GU_BuildingCursor;
		/**
		 *当前有点击到建筑项 
		 */
		private var _nowIsClickItem:Boolean	=	true;
		//		public var myllk:myLLK=null;
		
		public var moveHot:GV_ZombieHotPanel;	//移动热感 
		
		public function GV_Map():void
		{
			_instance=this;
			loadBg();
			this.init();
		}
		
		private function loadBg():void
		{
			new LoadImg().loadingImg(M_ConfigData.instance.bgpicUrl,setbgPic);
		}
		
		private function setbgPic(value:DisplayObject):void
		{
			this._layerMapBackground=value;
			this.layerMapBackground.x=-2000;
			this.addChildAt(value,0);
		}
		
		private function init():void
		{
			this._gridsAry=new Array;
			this._layerClick=new Sprite;
			this.addChild(this.layerClick);
			this._layerSundries=new Sprite;
			this.addChild(this.layerSundries);
			this.layerSundries.mouseEnabled=false;
			this._layerMapTargets=new Sprite;
			this.addChild(this.layerMapTargets);
			this.layerMapTargets.mouseEnabled=false;
			this._layerReward=new Sprite;
			this.addChild(this.layerReward);
			this.layerReward.mouseEnabled=false;
			this._layerBuildingSet=new GV_BuildingSetLayer;
			this.addChild(this.layerBuildingSet);
			this._popTipsLayer=new Sprite();
			this._popTipsLayer.mouseEnabled=false;
			this.addChild(this.popTipsLayer);
			this._rewardAry=new Array;
			this._needCheckWaitingSelectTargetSign=true;
			this.addListener();
			
			/** 2012.4.19 update by jamesge **/
			this._mapTargetsAry=new Array;
			this._floorTargetsAry=new Array;
			/** 2012.4.19 update by jamesge **/
		}
		
		
		/**
		 *地图更新 
		 * 2012.4.19 update by jamesge
		 * 
		 */		
		public function update():void
		{
			var timer1:Number=getTimer();
			this.frameNum+=1;
			var i:int;
			var mapTarget:MapTarget;
			for(i=0;i<this.layerReward.numChildren;i++)
			{
				var reward:VD_Reward=this.layerReward.getChildAt(i) as VD_Reward;
				reward.update();
			}
			for(i=0;i<this.mapTargetsAry.length;i++)
			{
				mapTarget=this.mapTargetsAry[i] as MapTarget;
				if(mapTarget is MapMoveTarget && this.buildMode==CANNOTBUILDMODE && !mapTarget.removeSign)
				{
					(mapTarget as MapMoveTarget).update();
				}
				if(mapTarget.removeSign==1)
				{
					this.mapTargetsAry.splice(i,1);
					i--;
					continue;
				}
				else if(mapTarget.removeSign==2)
				{
					mapTarget.redraw();
				}
			}
			
			for(i=0;i<this.floorTargetsAry.length;i++)
			{
				mapTarget=this.floorTargetsAry[i] as MapTarget;
				if(mapTarget.removeSign==1)
				{
					this.floorTargetsAry.splice(i,1);
					i--;
					//mapTarget.release();
					continue;
				}
				else if(mapTarget.removeSign==2)
				{
					mapTarget.redraw();
				}
			}
			
			/*判定当前选择对象*/
			if(this.needCheckWaitingSelectTargetSign)
			{
				if(this.waitingSelectTarget)
				{
					this.waitingSelectTarget.setGlowFilter(false);
				}
				this._waitingSelectTarget=null;
				var hitMouseTargetAry:Array=new Array;
				for each(mapTarget in this.mapTargetsAry)
				{
					
					mapTarget.setTransparentFromGVMap(false);
					if(mapTarget.hitTestPointCheck(this.stage.mouseX,this.stage.mouseY))
					{
						hitMouseTargetAry.push(mapTarget);
						mapTarget.setTransparentFromGVMap(true);
						if(!this.waitingSelectTarget || mapTarget.selectPriority>waitingSelectTarget.selectPriority)
						{
							this._waitingSelectTarget=mapTarget;
						}
					}
				}
				
				for each(mapTarget in this.floorTargetsAry)
				{
					mapTarget.setTransparentFromGVMap(false);
					if(mapTarget.hitTestPointCheck(this.stage.mouseX,this.stage.mouseY))
					{
						hitMouseTargetAry.push(mapTarget);
						mapTarget.setTransparentFromGVMap(true);
						if(!this.waitingSelectTarget || mapTarget.selectPriority>waitingSelectTarget.selectPriority)
						{
							this._waitingSelectTarget=mapTarget;
						}
					}
				}
				if(this.waitingSelectTarget)
				{
					this.waitingSelectTarget.setTransparentFromGVMap(false);
					this.waitingSelectTarget.setGlowFilter(true);
				}
			}
			
			
			
			
			var timer2:Number=getTimer();
			
			if(!(this.frameNum%10))
			{
				this.updateView();
				var timer3:Number=getTimer();
				
				if(!(this.frameNum%30))
				{
					//				    trace("timer1~2:"+(timer2-timer1)+"; timer2~3:"+(timer3-timer2)+";");
				}
			}
		}
		
		/** 2012.4.19 update by jamesge **/
		/**
		 *更新地图视图 
		 * 
		 */		
		private function updateView():void
		{
			var i:int;
			var mapTarget:MapTarget;
			var viewTargetsAry:Array=new Array;
			
			while(this.layerMapTargets.numChildren)
			{
				this.layerMapTargets.removeChildAt(0);
			}
			
			for(i=0;i<this.mapTargetsAry.length;i++)
			{
				mapTarget=this.mapTargetsAry[i] as MapTarget;
				if(this.checkInView(mapTarget))
				{
					viewTargetsAry.push(mapTarget);
				}
			}
			
			viewTargetsAry.sortOn(["deepIndex","deepIndexByX"],Array.NUMERIC);
			
			for(i=0;i<viewTargetsAry.length;i++)
			{
				mapTarget=viewTargetsAry[i] as MapTarget;
				this.layerMapTargets.addChild(mapTarget);
			}
			
		}
		
		/**
		 *检测地图显示对象是否在视野内 
		 * @param target
		 * @return 
		 * 
		 */		
		public function checkInView(target:MapTarget):Boolean
		{
			var globePoint:Point=this.localToGlobal(new Point(target.x,target.y));
			
			if(globePoint.x<-50 || globePoint.y<-50 || globePoint.x>stage.stageWidth-20 || globePoint.y>stage.stageHeight+40)
			{
				return false;
			}
			
			return true;
		}
		/** 2012.4.19 update by jamesge **/
		
		/**
		 *设定移动对象的可见性 
		 * @param value
		 * 
		 */		
		private function setMoveTargetsVisible(value:Boolean):void
		{
			for(var i:uint=0;i<this.mapTargetsAry.length;i++)
			{
				var mapTarget:MapTarget=this.mapTargetsAry[i] as MapTarget;
				if(mapTarget is MapMoveTarget)
				{
					mapTarget.visible=value;
				}
				
			}
			
			this.layerReward.visible=value;
		}
		
		/**
		 *创建地图 
		 * @param mapData 地图数据对象
		 * 
		 */			
		public function creat(mapData:GD_Map):void
		{
			this.reset();
			this._mapData=mapData;
			this.mapInit();
			this.layerBuildingSet.creat(this.width,this.height);
			this.creatMapTargets();
		}
		
		/**
		 *地图初始化（创建完成后调用） 
		 * 
		 */		
		private function mapInit():void
		{
			for(var i:int=0;i<this.heightNum;i++)
			{
				for(var j:int=0;j<this.widthNum;j++)
				{
					var grid:GD_MapGrid=GD_MapGrid.creat(i*this.widthNum+j,this.mapData);
					this.gridsAry.push(grid);
				}
			}
			this.creatClickLayer();
			//			this.dragRt=new Rectangle(760-this.width/2,600-this.height,this.width-760,this.height-600);
			this.dragRt=new Rectangle(this.stage.stageWidth-this.width/2,this.stage.stageHeight-this.height,this.width-stage.stageWidth,this.height-stage.stageHeight);
		}
		
		/**
		 *创建地图对象 
		 * 
		 */						
		public function creatMapTargets():void
		{
			this.wallLeavel=(this.mapData.wallsAry.length-72)/2+1;
			this.creatHaven(wallLeavel);
			this.creatWalls(this.mapData.wallsAry);
			this.creatBuildings(this.mapData.buildingsObj);
			this.creatSundries(this.mapData.sundriesObj);
			//this.creatDecorations(decorationsObj);
			this.creatSickMans(this.mapData.sickMansObj);
			this.creatZombies(this.mapData.zombiesObj);
			
			GD_HavenWall.setWallsVisible();
		}
		
		/**
		 *在拓展围墙时 移除以前的的围墙视图 
		 * @param wallsArr  存储GD_HavenWall数组
		 */				
		public function removeOldHavenView(wallsArr:Array):void
		{
			var havenObj:Object=getHavenObjByHavenLv(wallLeavel-1);
			for(var i:int=0;i<havenObj.height*2;i++)
			{
				for(var j:int=0;j<havenObj.width*2;j++)
				{
					var grid:GD_MapGrid=this.getCoordinateGrid(new Point(havenObj.atx+j,havenObj.aty+i));
					grid.isWall=false;
				}
			}
			
			for each(var checkWallData:GD_HavenWall in wallsArr)
			{
				var wallDisplayer:MT_HavenWall=checkWallData.view;
				//				this.layerMapTargets.removeChild(wallDisplayer);
				wallDisplayer.release();
			}
		}
		
		/**
		 * 创建避难所 
		 * @param havenLv 避难所等级 
		 */				
		public function creatHaven(havenLv:int):void
		{
			this.wallLeavel=havenLv;
			var havenObj:Object=getHavenObjByHavenLv(havenLv);
			for(var i:int=0;i<havenObj.height*2;i++)
			{
				for(var j:int=0;j<havenObj.width*2;j++)
				{
					var grid:GD_MapGrid=this.getCoordinateGrid(new Point(havenObj.atx+j,havenObj.aty+i));
					grid.isHaven=true;
				}
			}
		}
		
		/**
		 *创建避难所围墙 
		 * @param wallsAry 围墙数据数组
		 * 
		 */		
		public function creatWalls(wallsAry:Array):void
		{
			var havenObj:Object=getHavenObjByHavenLv(this.wallLeavel);
			var i:int;
			var j:int=0;
			var locationType:int;
			var wallData:GD_HavenWall;
			var atx:int;
			var aty:int;
			/*左上格*/
			locationType=1;
			atx=havenObj.atx;
			aty=havenObj.aty;
			
			wallData=wallsAry[j] as GD_HavenWall;
			wallData.setPositionData(atx,aty,locationType);
			j++;
			/*上格*/
			locationType=2;
			for(i=1;i<(havenObj.width-1);i++)
			{
				atx=havenObj.atx+(i*2);
				aty=havenObj.aty;
				wallData=wallsAry[j] as GD_HavenWall;
				wallData.setPositionData(atx,aty,locationType);
				j++;
			}
			
			/*右上格*/
			locationType=3;
			atx=havenObj.atx+havenObj.width*2-2;
			aty=havenObj.aty;
			wallData=wallsAry[j] as GD_HavenWall;
			wallData.setPositionData(atx,aty,locationType);
			j++;
			/*右格*/
			locationType=4;
			for(i=1;i<(havenObj.height-1);i++)
			{
				atx=havenObj.atx+havenObj.width*2-2;
				aty=havenObj.aty+(i*2);
				wallData=wallsAry[j] as GD_HavenWall;
				wallData.setPositionData(atx,aty,locationType);
				j++;
			}
			/*右下格*/
			locationType=5;
			atx=havenObj.atx+havenObj.width*2-2;
			aty=havenObj.aty+havenObj.height*2-2;
			wallData=wallsAry[j] as GD_HavenWall;
			wallData.setPositionData(atx,aty,locationType);
			j++;
			
			/*下格*/
			locationType=6;
			for(i=1;i<(havenObj.width-1);i++)
			{
				atx=havenObj.atx+havenObj.width*2-2-(i*2);
				aty=havenObj.aty+havenObj.height*2-2;
				wallData=wallsAry[j] as GD_HavenWall;
				wallData.setPositionData(atx,aty,locationType);
				j++;
			}
			/*左下格*/
			locationType=7;
			atx=havenObj.atx;
			aty=havenObj.aty+havenObj.height*2-2;
			wallData=wallsAry[j] as GD_HavenWall;
			wallData.setPositionData(atx,aty,locationType);
			j++;
			/*左格*/
			locationType=8;
			for(i=1;i<(havenObj.height-1);i++)
			{
				atx=havenObj.atx;
				aty=havenObj.aty+havenObj.height*2-2-(i*2);
				wallData=wallsAry[j] as GD_HavenWall;
				wallData.setPositionData(atx,aty,locationType);
				j++;
			}
			for each(var checkWallData:GD_HavenWall in wallsAry)
			{
				var wallDisplayer:MT_HavenWall=checkWallData.view;
				this.addTarget(wallDisplayer,this.getCoordinateGrid(checkWallData.positionPoint));
			}
		}
		
		/**
		 *根据避难所(即篱笆围起来的区域)等级获取避难所数据对象 
		 * @param havenLv 避难所等级
		 * @return 避难所数据对象
		 * 
		 */
		public static function getHavenObjByHavenLv(havenLv:int):Object
		{
			//地图单元格
			var baseX:int=82;
			var baseY:int=82;
			var baseW:int=19;
			var baseH:int=19;
			var extraX:int=0;
			var extraY:int=0;
			var extraW:int=0;
			var extraH:int=0;
			if(havenLv>1)
			{
				for(var i:int=0;i<havenLv-1;i++)
				{
					var tmpNum:int=i%4;
					var par:Number=havenSetConfigDataAry[i].zt_land_add;
					switch(tmpNum)
					{
						case 0: //像上拓展  
							extraY-=par*2;
							extraH+=par;
							break;
						case 1: //右 
							extraW+=par;
							break;
						case 2: //下
							extraH+=par;
							break;
						case 3: //左
							extraX-=par*2;
							extraW+=par;
							break; 
					}
				}
			}
			return {atx:baseX+extraX,aty:baseY+extraY,width:baseW+extraW,height:baseH+extraH};//是不是有问题
		}
		
		/**
		 *创建建筑物 
		 * @param buildingsObj 建筑数据存储器
		 * 
		 */		
		private function creatBuildings(buildingsObj:Object):void
		{
			for each(var buildingData:GD_Building_Func in buildingsObj)
			{
				this.creatBuilding(buildingData);
			}
		}
		
		/**
		 *创建杂物 
		 * @param sundriesObj 杂物数据存储器
		 * 
		 */		
		public function creatSundries(sundriesObj:Object):void
		{
			for each(var sundryData:GD_Building_Sundry in sundriesObj)
			{
				this.creatSundry(sundryData);
			}
		}
		
		/**
		 *创建装饰物 
		 * @param decorationsObj 装饰数据存储器
		 * 
		 */		
		private function creatDecorations(decorationsObj:Object):void
		{
			
		}
		
		/**
		 *创建病人 
		 * @param sickMansObj 病人数据存储器
		 * 
		 */		
		private function creatSickMans(sickMansObj:Object):void
		{
			var eligibleGridsAry:Array=this.getEligibleGridsAry(["避难所内","没有建筑","没有围墙"]);
			for each(var sickManData:GD_SickMan in sickMansObj)
			{
				var sickManDisplayer:MN_SickMan=sickManData.view;
				if(!eligibleGridsAry.length)throw new GameError("没有足够的位置放置对象");
				var randomIndex:int=Math.random()*eligibleGridsAry.length;
				var randomGrid:GD_MapGrid=eligibleGridsAry.splice(randomIndex,1)[0];
				this.addTarget(sickManDisplayer,randomGrid);
			}
		}
		
		/**
		 *创建僵尸 
		 * @param playerLv 僵尸数据存储器
		 * 
		 */		
		public function creatZombies(zombiesObj:Object):void
		{
			var eligibleGridsAry:Array;
			var eligibleGridsAry1:Array=this.getEligibleGridsAry(["避难所内","没有建筑","没有围墙","没有NPC"]);
			var eligibleGridsAry2:Array=this.getEligibleGridsAry(["避难所外","没有建筑"]);
			
			for each(var zombieData:GD_Zombie in zombiesObj)
			{
				eligibleGridsAry	=	(zombieData.zombieType.toString().slice(0,3) == "211")?eligibleGridsAry1:eligibleGridsAry2;
				
				var zombieDisplayer:MN_Zombie=zombieData.view;
				var randomGrid:GD_MapGrid;
				if(zombieData.zombieCoordinate==-1)
				{
					if(!eligibleGridsAry.length)throw new GameError("没有足够的位置放置对象");
					var randomIndex:int=Math.random()*eligibleGridsAry.length;
					randomGrid=eligibleGridsAry.splice(randomIndex,1)[0];
				}else{
					randomGrid=this.getCoordinateGrid(zombieData.positionPoint);
				}
				this.addTarget(zombieDisplayer,randomGrid);
			}
			if(GD_Player.instance.missions[11000001])
			{
				GD_HavenWall.inbreakCheck();
			}
		}
		
		/**
		 *根据建筑数据创建一个新建筑 
		 * @param buildingData 建筑数据对象
		 * @return 建筑显示对象
		 * 
		 */		
		public function creatBuilding(buildingData:GD_Building_Func):MT_Building
		{
			var buildingDisplayer:MT_Building=buildingData.view;
			this.addTarget(buildingDisplayer,this.getCoordinateGrid(buildingData.positionPoint));
			return buildingDisplayer;
		}
		
		/**
		 * 升级建筑 
		 * @param buildFunId
		 */		
		public function upBuilding(buildFunId:Number):void
		{
			for each(var buildingData:GD_Building_Func in this.mapData.buildingsObj)
			{
				if(buildingData.buildingType==buildFunId)
				{
					var oldBuildingDisplayer:MT_Building=buildingData.view;
					buildingData.buildingLv+=1;
					//					this.layerMapTargets.removeChild(oldBuildingDisplayer);
					oldBuildingDisplayer.removeSign=2;
					//oldBuildingDisplayer=null;
					//buildingData.release();
					//var newBuildingDisplayer:MT_Building=buildingData.view;   
					//this.addTarget(newBuildingDisplayer,this.getCoordinateGrid(buildingData.positionPoint));
					oldBuildingDisplayer.setState(10000001,true,1200);
					GU_Sound.instance.playCommSound("Button_Building_Upgrade");
					
					//促使任务进行刷新
					//					GD_Player.instance.changeMIssions(1,104,msgObj.value);//拥有建筑
					GD_Player.instance.changeBuildMiss(1,buildingData.buildingItemType);
					break;
				}
			}
		}
		
		/**
		 * 修理围墙 
		 * @param wallId
		 * @param dur
		 */		
		public function repairWalls(wallId:Number,dur:Number):void
		{
			for each(var wallData:GD_HavenWall in this.mapData.wallsAry)
			{
				if(wallData.wallSerial==wallId)
				{
					var oldHavenWallDisplayer:MT_HavenWall=wallData.view;
					wallData.dur=dur;
					//					this.layerMapTargets.removeChild(oldHavenWallDisplayer);
					oldHavenWallDisplayer.removeSign=2;
					//oldHavenWallDisplayer=null;
					//wallData.release();
					//var newHavenWallDisplayer:MT_HavenWall=wallData.view;     
					//this.addTarget(newHavenWallDisplayer,this.getCoordinateGrid(wallData.positionPoint));
					break;
				}
			}
		}
		
		/**
		 * 清理杂物 
		 * @param sundrySerialId
		 * @param dur
		 */		
		public function removeSundry(sundrySerialId:Number,dur:int):void
		{
			for each(var sundryData:GD_Building_Sundry in this.mapData.sundriesObj)
			{
				if(sundryData.serverID==sundrySerialId)
				{
					var oldSundryDisplayer:MT_Building_Sundry=sundryData.view;
					sundryData.sundryDur=dur;
					//					if(sundryData.isFloor)
					//					{
					//						this.layerSundries.removeChild(oldSundryDisplayer);
					//					}else{
					//						this.layerMapTargets.removeChild(oldSundryDisplayer);
					//					}	
					//					oldSundryDisplayer.removeSign=true;//
					//					oldSundryDisplayer=null;
					//					sundryData.release();
					if(dur>0)
					{
						oldSundryDisplayer.removeSign=2;
						//						var newSundryDisplayer:MT_Building_Sundry=sundryData.view;    //
						//						this.addTarget(newSundryDisplayer,this.getCoordinateGrid(sundryData.positionPoint),sundryData.isFloor);//
						//						if(sundryData.isFloor)
						//						{
						//							this.layerSundries.addChild(newSundryDisplayer);
						//							newSundryDisplayer.setPosition(this.getCoordinateGrid(sundryData.positionPoint));
						//						}else
						//						{
						//							this.addTarget(newSundryDisplayer,this.getCoordinateGrid(sundryData.positionPoint));
						//						}
					}else{
						sundryData.release();
					}
					break;
				}
			}
		}
		
		
		/**
		 *根据病人数据创建一个新病人 
		 * @param sickManData 病人数据对象
		 * @return 病人显示对象
		 * 
		 */		
		public function creatSickMan(sickManData:GD_SickMan):MN_SickMan
		{
			var sickManDisplayer:MN_SickMan=sickManData.view;
			var conditionsAry:Array=["避难所内","没有建筑","没有围墙","没有NPC"];
			var randomGrid:GD_MapGrid=this.getRandomGrid(conditionsAry);
			this.addTarget(sickManDisplayer,randomGrid);
			return sickManDisplayer;
		}
		
		/**
		 *根据僵尸数据创建一个新僵尸 
		 * @param zombieData 僵尸数据对象
		 * @return 僵尸显示对象
		 * 
		 */		
		public function creatZombie(zombieData:GD_Zombie):MN_Zombie
		{
			var zombieDisplayer:MN_Zombie=zombieData.view;
			var conditionsAry:Array=["避难所外","没有建筑","没有NPC"];
			var randomGrid:GD_MapGrid=this.getRandomGrid(conditionsAry);
			this.addTarget(zombieDisplayer,randomGrid);
			return zombieDisplayer;
		}
		
		/**
		 * 根据角色ID获取角色对象 
		 * @param id
		 * @return 
		 */		
		public function getPlayerControlTagetById(id:Number):MMT_Character
		{
			return this.controlTargetObj[id];
		}
		
		/**
		 *根据角色数据创建一个新角色 
		 * @param characterData 角色数据对象
		 * @return 角色显示对象
		 * 
		 */		
		public function creatCharacter(characterData:GD_Character,isController:Boolean):MMT_Character
		{
			var characterDisplayer:MMT_Character=characterData.view;
			//处理回放好友  使其随机生成到地图可视区域类
			var conditionsAry:Array;
			var randomGrid:GD_MapGrid;
			if(characterData.characterState==3)//回放好友
			{
				conditionsAry=["没有建筑","没有NPC"];
				randomGrid=this.getRandomGrid(conditionsAry,true);
			}else{
				conditionsAry=["避难所内","没有建筑"];
				randomGrid=this.getRandomGrid(conditionsAry);
			}
			
			this.addTarget(characterDisplayer,randomGrid);
			if(isController)
			{
				this.controlTarget=characterDisplayer;
				this.controlTarget.isControl=true;
				characterDisplayer.controlJianTouVisable(true);
			}
			this.controlTargetObj[characterData.characterId]=characterDisplayer;
			return characterDisplayer;
		}
		
		/**
		 * 为角色任务获取随机地图 
		 * @param characterData
		 * @return 
		 */		
		public function getRandomGridForcharacter(characterData:GD_Character):GD_MapGrid
		{
			var conditionsAry:Array;
			var randomGrid:GD_MapGrid;
			if(characterData.characterState==3)//回放好友
			{
				conditionsAry=["没有建筑","没有NPC"];
				randomGrid=this.getRandomGrid(conditionsAry,true);
			}else{
				conditionsAry=["避难所内","没有建筑"];
				randomGrid=this.getRandomGrid(conditionsAry);
			}
			return randomGrid;
		}
		
		/**
		 * 把角色对象从地图上面移除 
		 * @param characterData
		 */		
		public function removeCharacter(characterData:GD_Character):void
		{
			//var characterDisplayer:MMT_Character=characterData.view;
			//			this.layerMapTargets.removeChild(characterDisplayer);
			//characterDisplayer.removeSign=1;
			//if(characterDisplayer.parent)characterDisplayer.parent.removeChild(characterDisplayer);
			characterData.release();
			delete this.controlTargetObj[characterData.characterId]
			//characterDisplayer=null;
		}
		
		/**
		 *根据杂物数据创建一个杂物 
		 * @param sundryData 杂物数据
		 * @return 杂物显示对象
		 * 2012.4.19 update by jamesge
		 * 
		 */		
		public function creatSundry(sundryData:GD_Building_Sundry):MT_Building_Sundry
		{
			var sundryDisplayer:MT_Building_Sundry=sundryData.view;
			var mapGrid:GD_MapGrid=this.getCoordinateGrid(sundryData.positionPoint);
			this.addTarget(sundryDisplayer,mapGrid,sundryData.isFloor);
			/*if(sundryData.isFloor)
			{
			//this.layerSundries.addChild(sundryDisplayer);
			this.addTarget(sundryDisplayer,mapGrid,true);
			}
			else
			{
			//this.layerMapTargets.addChild(sundryDisplayer);
			this.addTarget(sundryDisplayer,mapGrid);
			}*/
			//			if(sundryData.mirrorPar==1)
			//			{
			//				sundryDisplayer.scaleX=-1;
			//			}
			//sundryDisplayer.setPosition(mapGrid);
			return sundryDisplayer;
		}
		
		/**
		 *创建掉落奖励 
		 * @param creatObj 创建对象
		 * @return 掉落奖励显示对象
		 * 
		 */		
		public function creatReward(creatObj:Object,actObj:Object):VD_Reward
		{
			//if(int(creatObj.type)==0)return null;
			var rewardData:GD_Reward=GD_Reward.creat(creatObj);
			var rewardDisplayer:VD_Reward=rewardData.view;
			this.layerReward.addChild(rewardDisplayer);
			rewardDisplayer.setDropping(actObj.atx,actObj.aty,actObj.vx,actObj.vy,actObj.excursiony);
			return rewardDisplayer;
		}
		
		/**
		 *加入地图对象 
		 * @param mapTarget 地图对象
		 * @param grid 加入位置 
		 * @isFloorTarget 是否为 类型为300的贴地面的对象  默认为否
		 * 2012.4.19 update by jamesge
		 * 
		 */		
		public function addTarget(mapTarget:MapTarget,grid:GD_MapGrid,isFloorTarget:Boolean=false,childIndex:Number=0):void
		{
			if(isFloorTarget)
			{
				this.floorTargetsAry.push(mapTarget);
				//				this.layerSundries.addChild(mapTarget);
				this.layerSundries.addChildAt(mapTarget,childIndex);
			}
			else
			{
				this.mapTargetsAry.push(mapTarget);
			}
			//this.layerMapTargets.addChild(mapTarget);
			mapTarget.setPosition(grid);
		}
		
		/**
		 *获得一个随机的地图格 
		 * @param conditionsAry 生成范围
		 * @param isPlayerFriend 是否是回放好友记录  默认为False
		 * @return 
		 * 
		 */	
		public function getRandomGrid(conditionsAry:Array,isPlayerFriend:Boolean=false):GD_MapGrid
		{
			var eligibleGridsAry:Array=this.getEligibleGridsAry(conditionsAry,isPlayerFriend);
			if(!eligibleGridsAry.length)throw new Error("没有足够的位置")
			var randomIndex:int=Math.random()*eligibleGridsAry.length;
			var randomGrid:GD_MapGrid=eligibleGridsAry[randomIndex];
			return randomGrid;
		}
		
		/**
		 * 获得符合条件的地图格数组 
		 * @param conditionsAry 条件数组
		 * @param isPlayerFriend  是否是回放好友记录  默认为False
		 * @return 符合条件的地图格数组
		 */		
		public function getEligibleGridsAry(conditionsAry:Array,isPlayerFriend:Boolean=false):Array
		{
			var returnAry:Array=new Array;
			for(var i:int=0;i<this.gridsAry.length;i++)
			{
				var checkGrid:GD_MapGrid=this.gridsAry[i];
				if(checkGrid.conditionsCheck(conditionsAry))
				{
					if(isPlayerFriend)
					{
						//当前随机的地图阁
						var tmpX:Number=checkGrid.atx;
						var tmpY:Number=checkGrid.aty;
						
						var charaterX:Number=this.controlTarget.mapGrid.atx;
						var charaterY:Number=this.controlTarget.mapGrid.aty;
						
						if(Math.abs(tmpX-charaterX)<=20 && Math.abs(tmpY-charaterY)<=20)
						{
							//是否能过达到围墙那里
//							var wallData:GD_HavenWall=this.getRamdonWall();
//							var findingAry:Array=GU_BarPathFinding.getPolicyPointsAry(new Point(tmpX,tmpY),wallData.view.mapGrid.coordinatePoint,this);
//							if(null==findingAry || findingAry.length<1)
//							{
//								returnAry.push(wallData.view.mapGrid);
//							}else{
								returnAry.push(checkGrid);
//							}
						}
					}else{
						returnAry.push(checkGrid);
					}
				}
			}
			return returnAry;
		}
		
		/**
		 *添加监听 
		 * 
		 */		
		public function addListener():void
		{
			this.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
			//this.pressTimer.addEventListener(TimerEvent.TIMER,dragSetHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandle);
			this.addEventListener(MouseEvent.ROLL_OUT,mouseOutHandle);
			this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
		}
		
		/**
		 *移除监听 
		 * 
		 */		
		public function removeListener():void
		{
			this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler);
			//this.pressTimer.removeEventListener(TimerEvent.TIMER,dragSetHandler);
			this.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
			this.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandle);
			this.removeEventListener(MouseEvent.ROLL_OUT,mouseOutHandle);
			this.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
		}
		
		private function enterFrameHandler(e:Event):void
		{
			this.update();
		}
		
		private function mouseMoveHandler(e:MouseEvent):void
		{
			this._needCheckWaitingSelectTargetSign=true;
		}
		
		private function mouseDownHandler(e:MouseEvent):void
		{
			this._oldPositionPoint=new Point(this.x,this.y);
			this.startDrag(false,this.dragRt);
		}
		
		private function mouseUpHandle(e:MouseEvent):void
		{
			if(this.oldPositionPoint)
			{
				this.stopDrag();
				var nowPositionPoint:Point=new Point(this.x,this.y);
				var dis:Number=Math.abs(this.x-this.oldPositionPoint.x)+Math.abs(this.y-this.oldPositionPoint.y);
				if(dis<3)
				{
					this.mouseClickHandle(e);
				}
				/*else
				{
				this.preventClick=true;
				}*/
				this._oldPositionPoint=null;
			}
		}
		
		private function mouseOutHandle(e:MouseEvent):void
		{
			this.stopDrag();
		}
		
		/**
		 *鼠标点击操作事件 
		 * @param e
		 * 
		 */		
		private function mouseClickHandle(e:MouseEvent):void
		{
//			VP_movement.instance.openPanel();
			if(this.layerBuildingSet.visible)
			{
				this.layerBuildingSet.clickSet();
				/*this.preventClick=true;*/
			}
			else if(this.waitingSelectTarget && this.waitingSelectTarget.removeSign!=1)
			{
				waitingSelectTarget.beClicked(e);
			}
			else
			{
				this.moveHandle(new Point(this.mouseX,this.mouseY));
			}
			
			VP_Main.instance.buildIsHaveOne();
			
		}
		
		/**
		 *对象点击检测 
		 * @return 
		 * 
		 */		
		private function clickTargetCheck():MapTarget
		{
			var i:int;
			for(i=this.layerMapTargets.numChildren-1;i>-1;i--)
			{
				var checkTarget:MapTarget=this.layerMapTargets.getChildAt(i) as MapTarget;
				if(checkTarget.hitTestPointCheck(this.stage.mouseX,this.stage.mouseY))
				{
					return checkTarget;
				}
			}
			return null;
		}
		
		/**
		 *移动操作 
		 * 
		 */		
		private function moveHandle(pt:Point):void
		{
			var isoPoint:Point=GU_IsoUtils.screenToIso(pt);
			var atx:int=isoPoint.x/this.gridSize;
			var aty:int=isoPoint.y/this.gridSize;
			var coordinatePoint:Point=new Point(atx,aty);
			if(this.checkPoint(coordinatePoint))
			{
				this.controlTarget.setMove(coordinatePoint);
			}
		}
		
		/**
		 *创建地图背景层 
		 * 
		 */		
		/*private function creatBackgroundLayer():void
		{
		this.layerMapBackground.graphics.clear();
		this.layerMapBackground.graphics.beginFill(0xffff00);
		var p1:Point=GL_IsoUtils.isoToScreen(new Point(this.widthNum*this.gridSize,0));
		var p2:Point=GL_IsoUtils.isoToScreen(new Point(this.widthNum*this.gridSize,this.heightNum*this.gridSize));
		var p3:Point=GL_IsoUtils.isoToScreen(new Point(0,this.heightNum*this.gridSize));
		this.layerMapBackground.graphics.lineTo(p1.x,p1.y);
		this.layerMapBackground.graphics.lineTo(p2.x,p2.y);
		this.layerMapBackground.graphics.lineTo(p3.x,p3.y);
		this.layerMapBackground.graphics.lineTo(0,0);
		this.layerMapBackground.graphics.endFill();
		}*/
		
		/**
		 *创建地图点击层 
		 * 
		 */		
		private function creatClickLayer():void
		{
			this.layerClick.graphics.clear();
			this.layerClick.graphics.beginFill(0x000000,0.1);
			this.layerClick.graphics.drawRect(-this.width/2,0,this.width,this.height);
			this.layerClick.graphics.endFill();
		}
		
		/**
		 *根据坐标点获得地图格 
		 * @param pt 坐标点
		 * @return 地图格
		 * 
		 */		
		public function getCoordinateGrid(pt:Point):GD_MapGrid
		{
			if(!checkPoint(pt))throw new GameError("目标点不在地图内");
			return this.gridsAry[this.getCoordinateValue(pt)] as GD_MapGrid
		}
		
		/**
		 *通过坐标点获得坐标值 
		 * @param pt 坐标点
		 * @return 坐标值
		 * 
		 */		
		//*********no
		public function getCoordinateValue(pt:Point):int
		{
			return pt.y*this.widthNum+pt.x;
		}
		
		/**
		 *检测目标点是否处于地图内 
		 * @param pt 检测点
		 * @return 是否处于地图内
		 * 
		 */		
		public function checkPoint(pt:Point):Boolean
		{
			if(pt.x<0 || pt.y<0 || pt.x>=this.widthNum || pt.y>=this.heightNum)
			{
				return false;
			}
			return true;
		}
		
		/**
		 *获得某个地图格的邻格数组 
		 * @param targetGrid 对象格
		 * @return 邻格数组
		 * 
		 */		
		public function getNeighbourGridsAry(targetGrid:GD_MapGrid):Array
		{
			if(targetGrid==null)return null;
			var neighbourPtAry:Array=new Array;
			var returnAry:Array=new Array;
			neighbourPtAry.push(new Point(targetGrid.atx,targetGrid.aty-1));
			neighbourPtAry.push(new Point(targetGrid.atx,targetGrid.aty+1));
			neighbourPtAry.push(new Point(targetGrid.atx-1,targetGrid.aty));
			neighbourPtAry.push(new Point(targetGrid.atx+1,targetGrid.aty));
			for each(var neighbourPt:Point in neighbourPtAry)
			{
				try
				{
					returnAry.push(this.getCoordinateGrid(neighbourPt));
				}
				catch(e:GameError)
				{
					
				}
			}
			return returnAry;
		}
		
		/**
		 *获得两点之间第一个障碍 
		 * @param point1 点1
		 * @param point2 点2
		 * @return 障碍
		 * 
		 */		
		public function getFirstBarBetweenTwoPoints(point1:Point,point2:Point,actTarget:I_Bar=null):I_Bar
		{
			var pointsAry:Array=this.getPointsAryBetweenTwoPoints(point1,point2);
			for(var i:int=0;i<pointsAry.length;i++)
			{
				var checkPoint:Point=pointsAry[i];
				var checkGrid:GD_MapGrid=this.getCoordinateGrid(checkPoint);
				if(checkGrid.building && checkGrid.building.buildingData && !checkGrid.building.buildingData.isFloor && actTarget!=checkGrid.building)return checkGrid.building as I_Bar;
			}
			return null;
		}
		
		/**
		 *获得两点之间直线距离 
		 * @param point1 点1
		 * @param point2 点2
		 * @return 距离
		 */		
		public function getFindingPriceBetweenTwoPoints(point1:Point,point2:Point):Number
		{
			var dis_x:int=Math.abs(point2.x-point1.x);
			var dis_y:int=Math.abs(point2.y-point1.y);
			var dis:int=dis_x>dis_y?dis_y:dis_x;
			return dis*SQRT2+dis_x+dis_y-2*dis;
		}
		
		/**
		 *获得两点之间的点数组 
		 * @param point1 点1
		 * @param point2 点2
		 * @return 点数组
		 * 
		 */		
		public function getPointsAryBetweenTwoPoints(point1:Point,point2:Point):Array
		{
			var returnAry:Array=new Array;
			if(point2.equals(point1))return returnAry;
			var dis_x:int=point2.x-point1.x;
			var dis_y:int=point2.y-point1.y;
			while(dis_x!=0 || dis_y!=0)
			{
				if(Math.abs(dis_y)>Math.abs(dis_x))
				{
					if(dis_y>0)
					{
						dis_y-=1;
					}
					else
					{
						dis_y+=1;
					}
				}
				else if(Math.abs(dis_y)<Math.abs(dis_x))
				{
					if(dis_x>0)
					{
						dis_x-=1;
					}
					else
					{
						dis_x+=1;
					}
				}
				else
				{
					if(dis_y>0)
					{
						dis_y-=1;
					}
					else
					{
						dis_y+=1;
					}
					if(dis_x>0)
					{
						dis_x-=1;
					}
					else
					{
						dis_x+=1;
					}
				}
				returnAry.push(new Point(point2.x-dis_x,point2.y-dis_y));
			}
			return returnAry;
		}
		
		/**
		 *移除抛出奖励 
		 * @param rewardData
		 * 
		 */		
		public function removeReward(rewardData:GD_Reward):void
		{
			for(var i:int=0;i<this.rewardAry.length;i++)
			{
				if(rewardData==this.rewardAry[i])
				{
					this.rewardAry.splice(i,1);
					return;
				}
			}
		}
		
		/**
		 *对象位置修正 
		 * 
		 */		
		public function targetsPositionCorrect():void
		{
			for(var i:int=0;i<this.layerMapTargets.numChildren;i++)
			{
				var mapTarget:MapTarget=this.layerMapTargets.getChildAt(i) as MapTarget;
				if(mapTarget is MapMoveTarget)
				{
					var mapMoveTarget:MapMoveTarget=mapTarget as MapMoveTarget;
					if(!mapMoveTarget.mapGrid.conditionsCheck(mapMoveTarget.correctPositionConditionsAry))
					{
						var randomGrid:GD_MapGrid=this.getRandomGrid(mapMoveTarget.resetPositionConditionsAry);
						mapMoveTarget.setPosition(randomGrid);
					}
				}
			}
		}
		
		/**
		 *开始建设功能 
		 * 
		 */		
		public function beginBuild():void
		{			
			this.setMoveTargetsVisible(false);
			this._buildMode=MOVEMODE;
			GU_Sound.instance.playCommSound("Button_Building_Open");
			
			this._buildingCursor		= 	new GU_BuildingCursor(MOVEMODE);	
			stage.addChild(this._buildingCursor);
			this._buildingCursor.start();
			
			VP_Main.instance.buildReShowHandler(null);
		}
		
		/**
		 *完成建设功能 
		 * 
		 */	
		public function finishBuildDown(e:Event):void
		{
			this.interuptBuildSet();
			
			var moneyO:uint=0;
			var moneyJ:uint=0;
			var moneyItemNum:uint=0;
			var itemName:String="";
			
			for each(var changeBuilding:GD_Building_Func in GR_Build.instance.changeBuildings)
			{
				switch(changeBuilding.buildType.changeType)
				{
					case BuildType.BUY:
						if(changeBuilding.moneyType==0){
							moneyO+=changeBuilding.bulidPrice;
						}
						else if(changeBuilding.moneyType==1){
							moneyJ+=changeBuilding.bulidPrice;
						}
						else{
							moneyItemNum+=changeBuilding.bulidPrice;
							if(itemName=="")
								itemName=GD_deal.instance.getDataById(Number(changeBuilding.moneyType)).itemName;
						}
						break;
					case BuildType.PLACE:
						break;
					case BuildType.MOVE:
						break;
					case BuildType.DELETE:
						break;
					case BuildType.INVALID:
						continue;
						break;
				}
				
			}
			if((moneyJ+moneyO+moneyItemNum)!=0)
			{
				VP_TextWinPanel.instance.openTextWindon(Language.buildConfirmhintTitle,Language.buildConfirmhint1+(moneyJ==0?"":Language.jb+moneyJ+",")+(moneyO==0?"":Language.ob+moneyO+",")+(moneyItemNum==0?"":itemName+moneyItemNum+",")+Language.buildConfirmhint2,finishBuildConfirm);
				e.stopPropagation();
			}
		}
		public function finishBuild():void
		{
			this._buildMode=CANNOTBUILDMODE;
			GU_Sound.instance.playCommSound("Button_Building_Close");
			
			if(this._buildingCursor!=null){
				this._buildingCursor.stop();
				this._buildingCursor=null;
			}
			GR_Build.instance.sendBuildRecord(beginMain);
			GU_dealPower.instance.clickBuildBtn();
		}
		public function finishBuildConfirm():void
		{
			finishBuild();
			var sp:Object	=	VP_Main.instance.interfaceUI;//["panelBuild"];
			if(sp["panelState"]!="buildSet")return;
			sp["showMainPanel"]();
			
			sp.dispatchEvent(new PanelEvent(PanelEvent.FINISHBUILD,true));
		}
		
		/**
		 *设置建筑光标移进，出，点击。时处理 
		 * @param state
		 * 
		 */
		public function changeCursor(state:String):void
		{
			if(this._buildingCursor==null)return;
			
			if(state=="over"){
				if(this._buildingCursor!=null){
					this._buildingCursor.hide();				
				}
			}
			else
				if(state=="down")
				{
					
					if(this._nowIsClickItem)	//如果点击到建筑项
					{
						this._buildMode			=	MOVEMODE;
						this._buildingCursor.mode	=	MOVEMODE;	
						this._nowIsClickItem	=	false;
					}
					else{
						this.interuptBuildSet();
					}
				}
				else
				{
					if(this._buildingCursor!=null){
						this._buildingCursor.show();
					}
					
				}
		}
		
		/**
		 *恢复建筑操作 
		 * 
		 */	
		public function recoverBuild():void
		{
			var str:String = "";
			for each(var changeBuilding:GD_Building_Func in GR_Build.instance.changeBuildings)
			str+=changeBuilding.buildType.changeType
			
			if(str!="")
			{	
				VP_TextWinPanel.instance.openTextWindon(Language.recoverBuildConfirmTitle,Language.recoverBuildConfirm,recoverBuildConfirm);
			}
			
			
		}
		public function recoverBuildConfirm():void
		{
			this.interuptBuildSet();
			GR_Build.instance.recoverAll();
			
			VP_Main.instance.buildIsHaveOne();
		}
		
		/**
		 *改变当前的建设模式 
		 * @param mode
		 * 
		 */		
		public function changeBuildMode(mode:String):void
		{
			this.interuptBuildSet();
			this._buildMode=mode;
			
			this._buildingCursor.mode	=	mode;
		}
		
		/**
		 *中断当前的建设功能 
		 * 
		 */		
		private function interuptBuildSet():void
		{
			if(this.layerBuildingSet.visible)this.layerBuildingSet.interuptSet();
		}
		
		/**
		 *开始主要功能 
		 * 
		 */		
		public function beginMain():void
		{
			this.setMoveTargetsVisible(true);
			this.targetsPositionCorrect();
			(this.controlTarget as MMT_Character).actionInterrupt();
			
		}
		
		//**************回放好友*********************
		/**
		 * 随机获取地图上面的僵尸 
		 * @return 
		 */		
		public function getRamdonZombie():GD_Zombie
		{
			//这个循环以后需要提取出去的
			var playTargerObj:MapTarget=(this.controlTarget as MMT_Character).actTarget;
			var tmpArr:Array=new Array();
			for each(var gd:GD_Zombie in this.mapData.zombiesObj)
			{
				if(playTargerObj as GD_Zombie && (playTargerObj as GD_Zombie).serverID==gd.serverID){continue;}
				tmpArr.push(gd);
			}
			return getRightZombie(tmpArr.length,tmpArr);
		}
		
		private function getRightZombie(len:Number,tmpArr:Array):GD_Zombie
		{
			var index:Number=Math.floor(Math.random()*len);
			var curZombie:GD_Zombie=tmpArr[index] as GD_Zombie;
			var playTargerObj:MapTarget=(this.controlTarget as MMT_Character).actTarget;
			if(tmpArr.length<2 && (curZombie.isDead || curZombie.view.actionName=="beHitted" ||(playTargerObj as GD_Zombie && (playTargerObj as GD_Zombie).serverID==curZombie.serverID)))
			{
				return null;
			}
			//如果僵尸死了，或者在被攻击，或者已经被当前玩家所选中，则再次遍历
			if(curZombie.isDead || curZombie.view.actionName=="beHitted" ||(playTargerObj as GD_Zombie && (playTargerObj as GD_Zombie).serverID==curZombie.serverID))
			{
				getRightZombie(len,tmpArr);
			}
			return curZombie;
		}
		
		/**
		 * 随机获取地图上面的杂物 
		 * @return 
		 */		
		public function getRamdonSundry():GD_Building_Sundry
		{
			//这个循环以后需要提取出去的
			var playTargerObj:MapTarget=(this.controlTarget as MMT_Character).actTarget;
			var tmpArr:Array=new Array();
			for each(var gd:GD_Building_Sundry in this.mapData.sundriesObj)
			{
				if(playTargerObj as GD_Building_Sundry && (playTargerObj as GD_Building_Sundry).serverID==gd.serverID){continue;}
				tmpArr.push(gd);
			}
			return getRightSundry(tmpArr.length,tmpArr);
		}
		
		private function getRightSundry(len:Number,tmpArr:Array):GD_Building_Sundry
		{
			var index:Number=Math.floor(Math.random()*len);
			var curSundry:GD_Building_Sundry=tmpArr[index] as GD_Building_Sundry;
			var playTargerObj:MapTarget=(this.controlTarget as MMT_Character).actTarget;
			if(tmpArr.length<2 && (playTargerObj as GD_Building_Sundry && (playTargerObj as GD_Building_Sundry).serverID==curSundry.serverID))
			{
				return null;
			}
			if(curSundry.sundryDur<1 || (playTargerObj as GD_Building_Sundry && (playTargerObj as GD_Building_Sundry).serverID==curSundry.serverID))
			{
				getRightSundry(len,tmpArr);
			}
			return curSundry;
		}
		
		/**
		 * 随机获取地图上面的围墙
		 * @return 
		 */		
		public function getRamdonWall():GD_HavenWall
		{
			//这个循环以后需要提取出去的
			var tmpArr:Array=this.mapData.wallsAry;
			
			var index:Number=Math.floor(Math.random()*tmpArr.length);
			var curWalls:GD_HavenWall=tmpArr[index] as GD_HavenWall;
			
			return curWalls;
		}
		
		
		//***********************************
		
		/**
		 * 释放资源   拜访好友 跟回家时
		 */		
		public function release_GVMap():void
		{
			this.removeListener();
			
//			this.removeChild(this.layerMapBackground);
//			this._layerMapBackground=null;
			
			var mapTarget:MapTarget;
			for each(mapTarget in this.mapTargetsAry)
			{
				if(mapTarget is MMT_Character && (mapTarget as MMT_Character).characterData.characterId==GD_Player.instance.playerID)
				{
					continue;
				}
				//释放掉其他的人物角色
				if(mapTarget is MMT_Character && (mapTarget as MMT_Character).characterData.characterId!=GD_Player.instance.playerID)
				{
					if((mapTarget as MMT_Character).characterData.characterState==1)
					{
						((mapTarget as MMT_Character).characterData.gd_characterPlay as GD_HiredFriend).copyLifeAndActBeforereleaseMyself();
					}
					((mapTarget as MMT_Character).characterData.gd_characterPlay as GD_HiredFriend).releaseHiredFriendGdCharate();
					continue;
				}
				mapTarget.release();
				if(mapTarget.gameData)mapTarget.gameData.release();
				
			}
			this.mapTargetsAry=[];
			for each(mapTarget in this.floorTargetsAry)
			{
				mapTarget.gameData.release();
			}
			this.floorTargetsAry=[];
			this.frameNum=0;
			/*for each(var mt:MMT_Character in this.controlTargetObj)
			{
			mt.actionName="";
			if(mt.characterData && mt.characterData.characterId!=GD_Player.instance.playerID)
			{
			this.removeCharacter(mt.characterData);
			//mt.release();
			if(mt.characterData)
			mt.characterData.release();
			}
			}
			//记得不要把自己删掉哦
			for(var i:int=0;i<this.mapTargetsAry.length;i++)
			{
			var mapt:MapTarget=this.mapTargetsAry[i] as MapTarget;
			if(mapt as MMT_Character)continue;
			
			mapt.release();
			if(mapt.gameData)mapt.gameData.release();
			mapt=null;
			}
			
			//地表对象
			for(var j:int=0;j<this.floorTargetsAry.length;j++)
			{
			var ms:MT_Building_Sundry=this.floorTargetsAry[j] as MT_Building_Sundry;
			ms.release();
			if(ms.buildingSundryData)	ms.buildingSundryData.release();
			ms=null;
			}*/
			this.mapData.gotoFriendHomeReleaseData();
			//while(this.layerClick.numChildren>0)this.layerClick.removeChildAt(0);
			//while(this.layerSundries.numChildren>0)this.layerSundries.removeChildAt(0);
			//while(this.layerMapTargets.numChildren>0)this.layerMapTargets.removeChildAt(0);
			//while(this.layerReward.numChildren>0)this.layerReward.removeChildAt(0);
			
			
			//			while(this.layerBuildingSet.numChildren>0)this.layerBuildingSet.removeChildAt(0);
			this.layerBuildingSet.reset();
			this.controlTarget=null;
			//this.controlTargetObj=null;
			this.controlTargetObj=new Object();
			//			this.removeListener();
		}
		
		/**
		 *复位 
		 * 
		 */		
		public function reset():void
		{
			if(this.mapData)
			{
				this.mapData.isShow=false;
				this._mapData=null;
			}
			for each(var grid:GD_MapGrid in this.gridsAry)
			{
				grid.release();
			}
			this._gridsAry=new Array;
			for each(var reward:GD_Reward in this.rewardAry)
			{
				reward.release();
			}
			this._rewardAry=new Array;
			this.layerBuildingSet.reset();
			while(this.layerMapTargets.numChildren)this.layerMapTargets.removeChildAt(0);
			//while(this.layerMapBackground.numChildren)this.layerMapBackground.removeChildAt(0);
			while(this.layerReward.numChildren)this.layerReward.removeChildAt(0);
		}
		
		/*getter & setter*/
		override public function get width():Number
		{
			return this.widthNum*this.gridSize*2;
		}
		
		override public function get height():Number
		{
			return this.heightNum*this.gridSize;
		}
		
		
		public static function get instance():GV_Map
		{
			return _instance;
		}
		
		public function get widthNum():int
		{
			return this.mapData.widthNum;
		}
		
		public function get heightNum():int
		{
			return this.mapData.heightNum;
		}
		
		public function get gridSize():Number
		{
			return _gridSize;
		}
		
		public function get gridsAry():Array
		{
			return _gridsAry;
		}
		
		public function get layerMapTargets():Sprite
		{
			return _layerMapTargets;
		}
		
		public function get layerMapBackground():DisplayObject
		{
			return _layerMapBackground;
		}
		
		public function get dragRt():Rectangle
		{
			return new Rectangle(this.stage.stageWidth-this.width/2,this.stage.stageHeight-this.height,this.width-stage.stageWidth,this.height-stage.stageHeight);
			//			return _dragRt;
		}
		
		public function set dragRt(value:Rectangle):void
		{
			_dragRt = value;
		}
		
		public function get layerBuildingSet():GV_BuildingSetLayer
		{
			return _layerBuildingSet;
		}
		
		public function get layerReward():Sprite
		{
			return _layerReward;
		}
		
		public function get rewardAry():Array
		{
			return _rewardAry;
		}
		
		public function get mapData():GD_Map
		{
			return _mapData;
		}
		
		public function get layerClick():Sprite
		{
			return _layerClick;
		}
		
		public function get controlTarget():I_ControlTarget
		{
			return _controlTarget;
		}
		
		public function set controlTarget(value:I_ControlTarget):void
		{
			_controlTarget = value;
		}
		
		public function get oldPositionPoint():Point
		{
			return _oldPositionPoint;
		}
		
		/*public function get preventClick():Boolean
		{
		return _preventClick;
		}
		
		public function set preventClick(value:Boolean):void
		{
		_preventClick = value;
		}*/
		
		public function set buildMode(value:String):void
		{
			_buildMode	=	value;
		}
		public function get buildMode():String
		{
			return _buildMode;
		}
		
		public static function get havenSetConfigDataAry():Array
		{
			if(!_havenSetConfigDataAry)
			{
				var havenSetConfigDataStr:String=M_ConfigData.instance.getCSVByName("避难所等级配置表");
				_havenSetConfigDataAry=GU_DataTransform.csvTransformObjsAry(havenSetConfigDataStr,8);
			}
			return _havenSetConfigDataAry;
		}
		
		public function get layerSundries():Sprite
		{
			return _layerSundries;
		}
		
		public function get waitingSelectTarget():MapTarget
		{
			return _waitingSelectTarget;
		}
		
		public function get needCheckWaitingSelectTargetSign():Boolean
		{
			return _needCheckWaitingSelectTargetSign;
		}
		
		public function get updateCount():Number
		{
			return _updateCount;
		}
		
		public function set updateCount(value:Number):void
		{
			_updateCount = value;
		}
		
		/**
		 * 玩家对象存储器 
		 */
		public function get controlTargetObj():Object
		{
			return _controlTargetObj;
		}
		
		/**
		 * @private
		 */
		public function set controlTargetObj(value:Object):void
		{
			_controlTargetObj = value;
		}
		
		/**
		 * 泡泡窗口层 
		 * @return 
		 */		
		public function get popTipsLayer():Sprite
		{
			return _popTipsLayer;
		}
		
		/**
		 * 围墙等级   好友与自己的共用 
		 */
		public function get wallLeavel():Number
		{
			return _wallLeavel;
		}
		
		/**
		 * @private
		 */
		public function set wallLeavel(value:Number):void
		{
			_wallLeavel = value;
		}
		
		
		/*public function get layerMapLines():Sprite
		{
		return _layerMapLines;
		}*/
		
		/*public function get layerMapGrids():Sprite
		{
		return _layerMapGrids;
		}*/
		
		/**
		 *鼠标移动到建筑面板上 
		 * @param e
		 * 
		 */
		private function changeCursorOverHandler(e:Event):void
		{
			GV_Map.instance.changeCursor("over");
		}
		/**
		 *鼠标离开建筑面板 
		 * @param e
		 * 
		 */
		private function changeCursorOutHandler(e:Event):void
		{
			GV_Map.instance.changeCursor("out");
			
		}
		/**
		 *单击建筑面板 
		 * @param e
		 * 
		 */
		private function changeCursorDownHandler(e:Event):void
		{
			GV_Map.instance.changeCursor("down");
			
		}
		
		/**
		 *鼠标有点击建筑项 
		 * @param e
		 * 
		 */
		public function isClickItem(e:Event):void
		{
			this._nowIsClickItem	=	true;
			
		}
		
		
		/** 2012.4.19 update by jamesge **/
		public function get mapTargetsAry():Array
		{
			return _mapTargetsAry;
		}
		
		public function set mapTargetsAry(value:Array):void
		{
			_mapTargetsAry = value;
		}
		
		public function get floorTargetsAry():Array
		{
			return _floorTargetsAry;
		}
		
		public function set floorTargetsAry(value:Array):void
		{
			_floorTargetsAry = value;
		}
		
		public function get frameNum():Number
		{
			return _frameNum;
		}
		
		public function set frameNum(value:Number):void
		{
			_frameNum = value;
		}
		
		
		/** 2012.4.19 update by jamesge **/
		
		
	}
}